package com.yellowpage.ca.action;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.yellowpage.white.common.Configuration;
import com.yellowpage.white.common.Constants;
import com.yellowpage.white.struts.BaseAction;
import com.yellowpage.white.struts.Module;
import com.yellowpage.white.struts.OperationType;
import com.yellowpage.white.util.Util4Java;

import com.yellowpage.ca.data.FuncModule;
import com.yellowpage.ca.data.Role;
import com.yellowpage.ca.service.FuncModuleService;
import com.yellowpage.ca.service.RoleService;

public class RoleAction extends BaseAction {
	private RoleService roleService;
	private FuncModuleService funcModuleService;

	public String RESULT_QUERY = "query";
	public String RESULT_ADD = "add";
	public String RESULT_EDIT = "edit";
	public String RESULT_USERS = "users";
	public String RESULT_FUNCS = "funcs";

	private Configuration configuration;

	private List<Role> roleList = new ArrayList<Role>();
	private List<Map> resultList = new ArrayList<Map>();

	private Role role;

	private String selectedId;
	private String[] selectedIds;
	private String message;

	private String query_name;
	private String query_type;

	private String[] roleUsers = new String[0];
	private List<Map> userWithRoleList = new ArrayList<Map>();
	private List<Map> userWithOutRoleList = new ArrayList<Map>();

	private String query_funcModuleId;
	private String[] roleFunctions = new String[0];
	private List<Map> funcWithRoleList = new ArrayList<Map>();
	private List<Map> funcWithOutRoleList = new ArrayList<Map>();
	public FuncModuleService getFuncModuleService() {
		return funcModuleService;
	}

	public void setFuncModuleService(FuncModuleService funcModuleService) {
		this.funcModuleService = funcModuleService;
	}

	public String getQuery_funcModuleId() {
		return query_funcModuleId;
	}

	public void setQuery_funcModuleId(String queryFuncModuleId) {
		query_funcModuleId = queryFuncModuleId;
	}

	private List<FuncModule> funcModuleList = new ArrayList<FuncModule>();

	public void prepare() {
		super.prepare();

		funcModuleList = funcModuleService.getAllFuncModule();

		if (this.selectedId != null && !this.selectedId.equals("")) {
			this.role = roleService.getRole(Long.parseLong(selectedId));
		}
	}

	public String queryByPage() {
		initPageRender();

		Map queryMap = new HashMap();

		queryMap.put("name", query_name);
		queryMap.put("type", query_type);

		resultList = roleService.queryRoleByPage(queryMap, pageRender);

		setPageRenderToRequest();

		return RESULT_QUERY;
	}

	public String toAdd() {
		return RESULT_ADD;
	}

	public String save() {
		role.setCreateTime(new Date());
		role.setCreateUser(loginUser.getUserId());
		role.setLastUpdateTime(new Date());
		role.setLastUpdateUser(loginUser.getUserId());
		role.setStatus("1");

		roleService.saveRole(role);

		message = "新增成功!";
		return RESULT_ADD;
	}

	public void validateSave() {
		if (role.getName().length() == 0) {
			this.addActionError("** Name 不能为空 !");
		}
		if (role.getType().length() == 0) {
			this.addActionError("** Type 不能为空 !");
		}

		this.setValidatorResult("/ca/RoleAdd.jsp");
	}

	public String toEdit() {
		Long id = new Long(selectedId);

		role = roleService.getRole(id);

		return RESULT_EDIT;
	}

	public String update() {
		role.setLastUpdateTime(new Date());
		role.setLastUpdateUser(loginUser.getUserId());

		roleService.updateRole(role);

		message = "修改成功!";

		return RESULT_EDIT;
	}

	public void validateUpdate() {
		if (role.getName().length() == 0) {
			this.addActionError("** Name 不能为空 !");
		}
		if (role.getType().length() == 0) {
			this.addActionError("** Type 不能为空 !");
		}
		this.setValidatorResult("/ca/RoleUpdate.jsp");
	}

	public String delete() {
		List ids = new ArrayList();

		for (String id : selectedIds) {
			ids.add(Long.valueOf(id));
		}

		this.roleService.delete(ids);

		return queryByPage();
	}

	public String toRoleFuncs() {
		Long roleId = new Long(selectedId);
		role = roleService.getRole(roleId);

		Map queryMap = new HashMap();
		queryMap.put("funcModuleId", query_funcModuleId);

		funcWithRoleList = roleService.getFunctionWithRole(roleId, true,
				queryMap);
		funcWithOutRoleList = roleService.getFunctionWithRole(roleId, false,
				queryMap);

		return RESULT_FUNCS;
	}

	public String saveFuncWithRole() {
		Long roleId = new Long(selectedId);
		
		Long funcModuleId =  null;
		if(query_funcModuleId != null && query_funcModuleId.length() > 0){
			funcModuleId = new Long(query_funcModuleId);
		}
		
		List<Long> ids = new ArrayList();
		for (String funcId : roleFunctions) {
			ids.add(Long.valueOf(funcId));
		}

		roleService.saveRoleFunctions(ids, roleId, loginUser.getUserId(),
				funcModuleId);

		role = roleService.getRole(roleId);

		Map queryMap = new HashMap();
		queryMap.put("funcModuleId", query_funcModuleId);

		funcWithRoleList = roleService.getFunctionWithRole(roleId, true,
				queryMap);
		funcWithOutRoleList = roleService.getFunctionWithRole(roleId, false,
				queryMap);

		return RESULT_FUNCS;
	}

	public String toRoleUsers() {
		Long roleId = new Long(selectedId);
		role = roleService.getRole(roleId);

		userWithRoleList = roleService.getUserWithRole(roleId, true);
		userWithOutRoleList = roleService.getUserWithRole(roleId, false);

		return RESULT_USERS;
	}

	public String saveUserWithRole() {
		Long roleId = new Long(selectedId);
		role = roleService.getRole(roleId);

		List<Long> ids = new ArrayList();
		for (String userId : roleUsers) {
			ids.add(Long.valueOf(userId));
		}

		roleService.saveRoleUsers(ids, roleId, loginUser.getUserId());

		userWithRoleList = roleService.getUserWithRole(roleId, true);
		userWithOutRoleList = roleService.getUserWithRole(roleId, false);

		return RESULT_USERS;
	}

	public String[] getRoleUsers() {
		return roleUsers;
	}

	public void setRoleUsers(String[] roleUsers) {
		this.roleUsers = roleUsers;
	}

	public List<Map> getUserWithRoleList() {
		return userWithRoleList;
	}

	public void setUserWithRoleList(List<Map> userWithRoleList) {
		this.userWithRoleList = userWithRoleList;
	}

	public List<Map> getUserWithOutRoleList() {
		return userWithOutRoleList;
	}

	public void setUserWithOutRoleList(List<Map> userWithOutRoleList) {
		this.userWithOutRoleList = userWithOutRoleList;
	}

	/**
	 * getters and setters
	 * 
	 * @return
	 */
	public void setRole(Role role) {
		this.role = role;
	}

	public Role getRole() {
		return role;
	}

	public void setRoleService(RoleService roleService) {
		this.roleService = roleService;
	}

	public List<Role> getRoleList() {
		return roleList;
	}

	public void setRoleList(List<Role> roleList) {
		this.roleList = roleList;
	}

	public String getSelectedId() {
		return selectedId;
	}

	public void setSelectedId(String selectedId) {
		this.selectedId = selectedId;
	}

	public String[] getSelectedIds() {
		return selectedIds;
	}

	public void setSelectedIds(String[] selectedIds) {
		this.selectedIds = selectedIds;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public String getQuery_name() {
		return this.query_name;
	}

	public void setQuery_name(String name) {
		this.query_name = name;
	}

	public String getQuery_type() {
		return this.query_type;
	}

	public void setQuery_type(String type) {
		this.query_type = type;
	}

	public List<Map> getResultList() {
		return resultList;
	}

	public void setResultList(List<Map> resultList) {
		this.resultList = resultList;
	}

	public String getQuery_funcModule() {
		return query_funcModuleId;
	}

	public void setQuery_funcModule(String queryFuncModule) {
		query_funcModuleId = queryFuncModule;
	}

	public String[] getRoleFunctions() {
		return roleFunctions;
	}

	public void setRoleFunctions(String[] roleFunctions) {
		this.roleFunctions = roleFunctions;
	}

	public List<Map> getFuncWithRoleList() {
		return funcWithRoleList;
	}

	public void setFuncWithRoleList(List<Map> funcWithRoleList) {
		this.funcWithRoleList = funcWithRoleList;
	}

	public List<Map> getFuncWithOutRoleList() {
		return funcWithOutRoleList;
	}

	public void setFuncWithOutRoleList(List<Map> funcWithOutRoleList) {
		this.funcWithOutRoleList = funcWithOutRoleList;
	}

	public List<FuncModule> getFuncModuleList() {
		return funcModuleList;
	}

	public void setFuncModuleList(List<FuncModule> funcModuleList) {
		this.funcModuleList = funcModuleList;
	}

}
